package com.ruoyi.zsyh.controller;

import java.util.List;

import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.zsyh.domain.ZsyhUserRakearea;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsyh.domain.ZsyhUserRepairarea;
import com.ruoyi.zsyh.service.IZsyhUserRepairareaService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 维修店服务区域Controller
 * 
 * @author yuexl
 * @date 2021-12-21
 */
@Controller
@RequestMapping("/zsyh/repairArea")
public class ZsyhUserRepairareaController extends BaseController
{
    private String prefix = "zsyh/repairArea";

    @Autowired
    private IZsyhUserRepairareaService zsyhUserRepairareaService;

    @RequiresPermissions("zsyh:repairArea:view")
    @GetMapping()
    public String repairArea()
    {
        return prefix + "/repairArea";
    }

    @RequiresPermissions("zsyh:repairArea:repairAreaView")
    @GetMapping("/repairAreaView")
    public String repairAreaView()
    {
        return prefix + "/repairAreaView";
    }

    /**
     * 查询维修店服务区域列表
     */
    @RequiresPermissions("zsyh:repairArea:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZsyhUserRepairarea zsyhUserRepairarea)
    {
        startPage();
        List<ZsyhUserRepairarea> list = zsyhUserRepairareaService.selectZsyhUserRepairareaList(zsyhUserRepairarea);
        return getDataTable(list);
    }

    /**
     * 查询维修店服务区域列表
     */
    @RequiresPermissions("zsyh:repairArea:list")
    @PostMapping("/listView")
    @ResponseBody
    public TableDataInfo listView(ZsyhUserRepairarea zsyhUserRepairarea)
    {
        startPage();
        List<ZsyhUserRepairarea> list = zsyhUserRepairareaService.selectZsyhUserRepairareaList(zsyhUserRepairarea);
        return getDataTable(list);
    }

    /**
     * 导出维修店服务区域列表
     */
    @RequiresPermissions("zsyh:repairArea:export")
    @Log(title = "维修店服务区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhUserRepairarea zsyhUserRepairarea)
    {
        List<ZsyhUserRepairarea> list = zsyhUserRepairareaService.selectZsyhUserRepairareaList(zsyhUserRepairarea);
        ExcelUtil<ZsyhUserRepairarea> util = new ExcelUtil<ZsyhUserRepairarea>(ZsyhUserRepairarea.class);
        return util.exportExcel(list, "维修店服务区域数据");
    }

    /**
     * 新增订单管理人管辖区域
     */
    @GetMapping("/add/{repairId}")
    public String add(@PathVariable("repairId") String repairId, ModelMap mmap)
    {
        mmap.put("repairId", repairId);
        return prefix + "/add";
    }

    /**
     * 新增保存维修店服务区域
     */
    @RequiresPermissions("zsyh:repairArea:add")
    @Log(title = "维修店服务区域", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhUserRepairarea zsyhUserRepairarea)
    {
        zsyhUserRepairarea.setId(IdWorker.getId());
        return toAjax(zsyhUserRepairareaService.insertZsyhUserRepairarea(zsyhUserRepairarea));
    }

    /**
     * 新增网点管辖区域
     */
    @RequiresPermissions("zsyh:repairArea:add")
    @Log(title = "维修店服务区域", businessType = BusinessType.INSERT)
    @PostMapping("/addRakerArea")
    @ResponseBody
    public AjaxResult addRakerArea(ZsyhUserRepairarea zsyhUserRepairarea)
    {
        //判断该区域是否有管理员
        ZsyhUserRepairarea zsyhUserRepairarea1 = new ZsyhUserRepairarea();
        zsyhUserRepairarea1.setType("2");
        zsyhUserRepairarea1.setRepairProvince(zsyhUserRepairarea.getRepairProvince());
        zsyhUserRepairarea1.setRepairCity(zsyhUserRepairarea.getRepairCity());
        zsyhUserRepairarea1.setRepairDistrict(zsyhUserRepairarea.getRepairDistrict());
        List<ZsyhUserRepairarea> zsyhUserRepairareaList = zsyhUserRepairareaService.selectZsyhUserRepairareaList(zsyhUserRepairarea1);
        if(zsyhUserRepairareaList.size()>0){
            return error("该区域已经有管理员，请切换其他区域再添加!");
        }

        zsyhUserRepairarea.setId(IdWorker.getId());
        return toAjax(zsyhUserRepairareaService.insertZsyhUserRepairarea(zsyhUserRepairarea));
    }

    /**
     * 修改维修店服务区域
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhUserRepairarea zsyhUserRepairarea = zsyhUserRepairareaService.selectZsyhUserRepairareaById(id);
        mmap.put("zsyhUserRepairarea", zsyhUserRepairarea);
        return prefix + "/edit";
    }

    /**
     * 修改保存维修店服务区域
     */
    @RequiresPermissions("zsyh:repairArea:edit")
    @Log(title = "维修店服务区域", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhUserRepairarea zsyhUserRepairarea)
    {
        return toAjax(zsyhUserRepairareaService.updateZsyhUserRepairarea(zsyhUserRepairarea));
    }

    /**
     * 删除维修店服务区域
     */
    @RequiresPermissions("zsyh:repairArea:remove")
    @Log(title = "维修店服务区域", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zsyhUserRepairareaService.deleteZsyhUserRepairareaByIds(ids));
    }
}
